package com.zjy.org.controller;

import com.zjy.basic.util.ExcelUtils;
import com.zjy.org.domain.ShopAuditLog;
import com.zjy.org.service.ShopService;
import com.zjy.org.domain.Shop;
import com.zjy.org.query.ShopQuery;
import com.zjy.basic.util.AjaxResult;
import com.zjy.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.mail.MessagingException;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 后端接口类；
 */
@RestController
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    public ShopService shopService;

    /**
     * 接口：添加或修改
     *
     * @param shop 传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult save(@RequestBody Shop shop) {
        try {
            if (shop.getId() != null)
                shopService.update(shop);
            else
                shopService.add(shop);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "操作失败");
        }
    }

    /**
     * 接口：删除
     *
     * @param id
     * @return AjaxResult 响应给前端
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult del(@PathVariable("id") Long id) {
        try {
            shopService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败");
        }
    }

    /**
     * 接口：批量删除
     *
     * @param ids
     * @return AjaxResult 响应给前端
     */
    @PatchMapping
    public AjaxResult patchDel(@RequestBody Long[] ids) {
        try {
            shopService.patchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "批量删除失败");
        }
    }

    /**
     * 接口：查询单个对象
     *
     * @param id
     */
    @GetMapping("/{id}")
    public Shop findById(@PathVariable("id") Long id) {
        return shopService.findById(id);
    }


    /**
     * 接口：查询所有
     *
     * @return
     */
    @GetMapping
    public List<Shop> findAll() {
        return shopService.findAll();
    }


    /**
     * 接口：分页查询或高级查询
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping
    public PageList<Shop> queryPage(@RequestBody ShopQuery query) {
        return shopService.queryPage(query);
    }

    /**
     * 注册
     *
     * @param shop
     * @return
     */
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop) {
        shopService.settlement(shop);
        return AjaxResult.me();
    }

    /**
     * 审核通过
     *
     * @param log
     * @return
     */
    @PostMapping("/pass")
    public AjaxResult pass(@RequestBody ShopAuditLog log) throws MessagingException {
        shopService.pass(log);
        return AjaxResult.me();
    }

    /**
     * 审核驳回
     *
     * @param log
     * @return
     */
    @PostMapping("/reject")
    public AjaxResult reject(@RequestBody ShopAuditLog log) {
        shopService.reject(log);
        return AjaxResult.me();
    }

    /**
     * excel导出
     *
     * @param response
     */
    @GetMapping("/export")
    public void exportExcel(HttpServletResponse response) {
        List<Shop> shops = shopService.findAll();
        ExcelUtils.exportExcel(shops, null, "店铺", Shop.class, "店铺信息.xls", response);
    }

    /**
     * excel导入
     *
     * @param file
     */
    @PostMapping("/import")
    public void importExcel(@RequestPart MultipartFile file) {
        List<Shop> shops = ExcelUtils.importExcel(file, 0, 1, Shop.class);
        shops.forEach(System.out::println);
        for (Shop shop : shops) {
            shopService.add(shop);
        }
    }

    /**
     * @return
     */
    @GetMapping("/statistics")
    public AjaxResult statistics() {
        return shopService.statistics();
    }

    @GetMapping("/active/{id}")
    public AjaxResult active(@PathVariable("id") Long id) {
        Shop shop = shopService.findById(id);
        shopService.adtive(shop);
        return AjaxResult.me();
    }
}
